package lzupdater.gui.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import lzupdater.timing.CentralTimer;
import lzupdater.timing.TimedTask;
import lzupdater.timing.TimerMode;
import lzupdater.utils.Profile;
import lzupdater.utils.Profile.ProfilePropertyId;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

public class LZFieldListener implements ActionListener, ProfileListener,
		DocumentListener, TimedTask {

	protected Profile profile;
	private final JTextField field;
	private final ProfilePropertyId propertyId;
	private final boolean noTimer;

	public LZFieldListener(JTextField field, ProfilePropertyId propertyId) {
		this(field, propertyId, false);
	}

	public LZFieldListener(JTextField field, ProfilePropertyId propertyId,
			boolean noTimer) {
		this.field = field;
		this.propertyId = propertyId;
		this.noTimer = noTimer;
		setProfile(null);
	}

	// TimedTask interface
	public void taskStopped() {
	}

	public void taskDone() {
	}

	public void performTask(long time) {
		setProperty(field.getText());
	}

	public void updateAsked() {
		setProperty(field.getText());
	}

	public void changedUpdate(DocumentEvent e) {}
	public void insertUpdate(DocumentEvent e) {
		if (noTimer) {
			setProperty(field.getText());
		} else {
			CentralTimer.resetTimedTask(this, 500.0, TimerMode.onlyOnce());
		}
	}
	public void removeUpdate(DocumentEvent e) {
		if (noTimer) {
			setProperty(field.getText());
		} else {
			CentralTimer.resetTimedTask(this, 500.0, TimerMode.onlyOnce());
		}
	}

	public void windowIsClosing() {
		setProperty(field.getText());
	}

	public void setBrowseValue(String text) {
		field.setText(text);
		setProperty(text);
	}

	public void actionPerformed(ActionEvent e) {
		setProperty(e.getActionCommand());
	}

	private void setProperty(String value) {
		CentralTimer.stopTimedTask(this);
		if (profile != null) {
			initialiseProperty(profile, propertyId, value);
		}
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
		field.setEnabled(profile != null);
		if (profile != null) {
			field.setText(profile.getProperty(propertyId));
		} else {
			field.setText("");
		}
	}

	protected void initialiseProperty(Profile profile,
			ProfilePropertyId propertyId, String value) {
		profile.setProperty(propertyId, value);
	}

	public Profile getProfile() {
		return profile;
	}

}
